<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * This file contains the PDODB_Manager_mysql class.
 *
 * PHP version 5
 *
 * This file is part of PEAR::PDODB - A database abstraction layer for PHP5 built
 * using PDO. PHP version 5.1+ is recommended.
 *
 * PEAR::PDODB is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * PEAR::PDODB is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with PEAR::PDODB. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package     PDODB
 * @subpackage  mysql
 * @category    Database
 * @author      Lukas Smith <smith@pooteeweet.org>
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 * @copyright   1998-2007 Manuel Lemos, Tomas V.V. Cox, Stig S. Bakken, Lukas Smith
 * @copyright   2007 Michael J. I. Jackson
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL 3.0
 * @version     SVN: $Id: mysql.php 2 2007-11-27 17:20:29Z mjijackson $
 * @link        http://pear.php.net/packages/PDODB
 */

/**
 * Require PDODB_Manager
 */
require_once dirname(__FILE__) . '.php';

/**
 * A MySQL-specific database manager module class.
 *
 * @package     PDODB
 * @category    Database
 * @author      Lukas Smith <smith@pooteeweet.org>
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 */
class PDODB_Manager_mysql extends PDODB_Manager
{

    /**
     * @see     PDODB_Manager::createDatabase()
     */
    public function createDatabase($name)
    {
        $db = $this->getConnection();
        return $db->exec('CREATE DATABASE ' . $db->quoteIdentifier($name, true));
    }

    /**
     * @see     PDODB_Manager::dropDatabase()
     */
    public function dropDatabase($name)
    {
        $db = $this->getConnection();
        return $db->exec('DROP DATABASE ' . $db->quoteIdentifier($name, true));
    }

    /**
     * @see     PDODB_Manager::getCreateTableQuery()
     */
    public function getCreateTableQuery($name, $fields, $options = array())
    {
        $db = $this->getConnection();
        $query = parent::getCreateTableQuery($name, $fields, $options);

        $options_strings = array();

        if (!empty($options['comment'])) {
            $options_strings['comment'] = 'COMMENT = ' . $db->quote($options['comment']);
        }
        if (!empty($options['charset'])) {
            $options_strings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset'];
            if (!empty($options['collate'])) {
                $options_strings['charset'].= ' COLLATE ' . $options['collate'];
            }
        }

        $type = empty($options['type'])
            ? $db->getAttribute(PDODB::ATTR_DEFAULT_TABLE_TYPE)
            : $options['type'];
        if (!empty($type)) {
            $options_strings[] = "ENGINE = $type";
        }

        if (!empty($options_strings)) {
            $query .= ' ' . implode(' ', $options_strings);
        }

        return $query;
    }

    /**
     * @see     PDODB_Connection::createSequence()
     */
    public function createSequence($seq_name, $start = 1, $options = array())
    {
        $db = $this->getConnection();

        $sequence_name = $db->quoteIdentifier($db->sequenceName($seq_name), true);
        $seqcol_name = $db->quoteIdentifier($db->getAttribute(PDODB::ATTR_SEQCOL_NAME), true);

        $options_strings = array();

        if (!empty($options['comment'])) {
            $options_strings['comment'] = 'COMMENT = '.$db->quote($options['comment'], 'text');
        }
        if (!empty($options['charset'])) {
            $options_strings['charset'] = 'DEFAULT CHARACTER SET '.$options['charset'];
            if (!empty($options['collate'])) {
                $options_strings['charset'].= ' COLLATE '.$options['collate'];
            }
        }

        $type = false;
        if (!empty($options['type'])) {
            $type = $options['type'];
        } elseif ($db->getAttribute(PDODB::ATTR_DEFAULT_TABLE_TYPE)) {
            $type = $db->getAttribute(PDODB::ATTR_DEFAULT_TABLE_TYPE);
        }
        if ($type) {
            $options_strings[] = "ENGINE = $type";
        }

        try {
            $query = "CREATE TABLE $sequence_name ($seqcol_name INT NOT NULL"
                . " AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))";
            if (!empty($options_strings)) {
                $query .= ' '.implode(' ', $options_strings);
            }
            $db->exec($query);
        } catch (PDODB_Exception $e) {
            throw new PDODB_CannotCreateException(
                "Could not create sequence $sequence_name", $e, $query);
        }

        if ($start == 1) {
            return;
        }

        try {
            $query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')';
            $res = $db->exec($query);
        } catch (PDODB_Exception $e) {
            try {
                $query = "DROP TABLE $sequence_name";
                $db->exec($query);
            } catch (PDODB_Exception $e) {
                throw new PDODB_CannotDropException(
                    "Could not drop inconsistent sequence table $sequence_name",
                    $e, $query);
            }

            throw new PDODB_CannotCreateException(
                "Could not create sequence $sequence_name", $e, $query);
        }
    }

    /**
     * @see     PDODB_Connection::dropSequence()
     */
    public function dropSequence($seq_name)
    {
        $db = $this->getConnection();
        $sequence_name = $db->quoteIdentifier($db->sequenceName($seq_name), true);

        return $db->exec("DROP TABLE $sequence_name");
    }

}

?>
